package com.hrbxlh.action;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.ProfessDirectionActionImpl;
import com.hrbxlh.actionImpl.ProfessionActionImpl;
import com.hrbxlh.actionImpl.SiteActionImpl;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：ProfessionAction.java
* Description：专业action类
* History：
* Date           Author               Desc
* 2017.04.11	  xzx                  建立	
* -------------------------------------------------------
*/

@Controller
public class ProfessionAction {
	
	@Autowired
	@Qualifier("professionActionImpl")
	private ProfessionActionImpl professionActionImpl;
	
	@Autowired
	@Qualifier("professDirectionActionImpl")
	private ProfessDirectionActionImpl professDirectionActionImpl;
	
	@Autowired
	@Qualifier("siteActionImpl")
	private SiteActionImpl siteActionImpl;
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	/**
	 * @description 删除专业(批量)
	 * @param response
	 * @param jsonStr List<String>形式   jsonStr = ["1","2","3"...]
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delProfessions", method = RequestMethod.POST)
	public void delProfessions(String jsonStr, HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {			
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
            //删除
			Map<String, Object> result = professionActionImpl.delProfessions(paramList);
			boolean resultMarkUse = (Boolean)result.get("result");
			//返回参数
			resultMark = (resultMarkUse) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(!resultMarkUse){
				resultMap.put("failMsg",result.get("failMsg"));
			}
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("删除专业出现异常delProfessions", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 跳转至添加专业页面 带全部地点信息
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipAddProfession", method = RequestMethod.POST)
	public void skipAddProfession(String jsonStr, HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取全部地点信息
			List<Map<String, Object>> siteList = siteActionImpl.getSites();
			resultMap.put("siteList", siteList);
			//返回参数
			resultMap.put("departmentId", paramMap.get("departmentId"));
			resultMap.put("result", "success");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("跳转添加专业页面出现异常skipAddProfession", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 添加专业  专业，专业方向，地点
		插入专业表  专业看是不是重名了
		得到专业主键id
		插入专业方向表带着专业id  需要判断专业方向名字是否存在
		插入专业地点关联
	 * @param response
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addProfession", method = RequestMethod.POST)
	public void addProfession(String jsonStr, HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if(courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
			//判断专业方向重复问题
			Map<String, Object> repeatMap = professDirectionActionImpl.getDirectRepeatMark(paramMap, 0);
			if(repeatMap.get("result").toString().equals("fail")){
				resultMap = repeatMap;
				courseActionImpl.responseStream(response, mapper.writeValueAsString(repeatMap));
				return;
			}
			Map<String, Object> professionAddResult = professionActionImpl.addProfession(paramMap);
			boolean addProfession = (Boolean)professionAddResult.get("result");
			//添加专业成功
			if(addProfession){
				paramMap.put("professionId", professionAddResult.get("professionId"));
				//添加专业方向
				Map<String, Object> addDirectResult = professDirectionActionImpl.addDirect(paramMap);
				if((Boolean)addDirectResult.get("result")){
					//添加专业与地点的关联
					boolean addSiteReProfession = siteActionImpl.addSiteReProfession(paramMap);
					if(addSiteReProfession){
						resultMap.put("result", "success");
					}else{
						resultMap.put("result", "fail");
						resultMap.put("failMsg", "添加失败");
					}
				}
				resultMap.put("result", "success");
				resultMap.put("professionId", professionAddResult.get("professionId"));
			}else{
				resultMap.put("result", "fail");
				resultMap.put("failMsg", professionAddResult.get("failMsg"));
			}
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("添加专业出现异常addProfession", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * @description 添加专业	下半部分  添加专业与地点的关联
	 * */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addProfessionReSite", method = RequestMethod.POST)
	public void addProfessionReSite(String jsonStr, HttpServletResponse response){
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		try {
			resultMap = mapper.readValue(jsonStr, resultMap.getClass());
			//添加教学地点与专业的关系
			boolean addSiteReProfession = siteActionImpl.addSiteReProfession(resultMap);
			if(addSiteReProfession){
				resultMap.put("result", "success");
			}else{
				resultMap.put("result", "fail");
				resultMap.put("failMsg", "添加失败");
			}
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "fail");
			logger.error("添加专业地点失败addProfessionReSite", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}



	/**
	 * @description 跳转至修改专业页面 带全部地点信息
	 * @param request  专业id
	 * @param response
	 * @return  跳转到修改专业页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveProfession", method = RequestMethod.POST)
	public void skipSaveProfession(String jsonStr, HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取全部地点信息
			List<Map<String, Object>> siteList = siteActionImpl.getSites();
			resultMap.put("allSiteList", siteList);
			//获取专业信息
			Map<String,Object> professionMap = professionActionImpl.getProfessionDetail(paramMap);
			resultMap.put("profession", professionMap);
			//获取专业的专业方向信息
			List<Map<String, Object>> directList = professDirectionActionImpl.getDirectByprofessId(paramMap);
			resultMap.put("directList", directList);
			//获取专业的关联地点信息
			List<Map<String, Object>> mySiteList = siteActionImpl.getSiteReProfession(paramMap);
			resultMap.put("mySiteList", mySiteList);
			resultMap.put("result", "success");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("跳转修改专业页面出现异常skipSaveProfession", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 修改专业		 上半部分
		修改专业表  专业看是不是重名了
		得到专业主键id
		插入专业方向表带着专业id  需要判断专业方向名字是否存在
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveProfession", method = RequestMethod.POST)
	public void saveProfession(String jsonStr, HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if(courseActionImpl.getMapSpecialMark(response, mapper, resultMap, paramMap)) return;
			//判断专业方向重复问题
			Map<String, Object> repeatMap = professDirectionActionImpl.getDirectRepeatMark(paramMap, 1);
			if(repeatMap.get("result").toString().equals("fail")){
				courseActionImpl.responseStream(response, mapper.writeValueAsString(repeatMap));
				return;
			}
			//判断是否有不能删除的专业方向
			if(paramMap.get("delList") != null && ((List<Integer>)paramMap.get("delList")).size() != 0){
				Map<String, Object> delDirectMarkMap = professDirectionActionImpl.delDirectListMark((List<Integer>)paramMap.get("delList"));
				if(delDirectMarkMap.get("result").toString().equals("fail")){
					courseActionImpl.responseStream(response, mapper.writeValueAsString(delDirectMarkMap));
					return;
				}
			}
			Map<String, Object> professionSaveResult = professionActionImpl.saveProfession(paramMap);
			boolean saveProfession = (Boolean)professionSaveResult.get("result");
			//修改专业成功
			if(saveProfession){
				Map<String, Object> saveDirectResult = professDirectionActionImpl.saveDirectList(paramMap);
				if((Boolean)saveDirectResult.get("result")){
					//删除要删除的专业方向
					Map<String, Object> delDirectMark = professDirectionActionImpl.delDirect(paramMap);
					if((Boolean)delDirectMark.get("result")){
						resultMap.put("result", "success");
					}else{
						resultMap.put("result", "fail");
					}
				}else{
					resultMap.put("result", "fail");
					resultMap.put("failMsg", saveDirectResult.get("failMsg"));
				}
			}else{
				resultMap.put("result", "fail");
				resultMap.put("failMsg", professionSaveResult.get("failMsg"));
			}
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("修改专业出现异常saveProfession", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 获取删除专业方向标志（是否可删除）
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delProfessDirection", method = RequestMethod.POST)
	public void delProfessDirection(String jsonStr , HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //获取删除标志
			Map<String, Object> resultMarkUse = professDirectionActionImpl.delDirectMark(paramMap);
			resultMap.putAll(resultMarkUse);
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("删除专业方向出现异常delProfessDirection", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
}